<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<table>
			<tr>
			<td colspan="3">
				<input type="text" id="numId">
			</td>
			<td>
				<input type="button" value="阶乘" onclick="factorial()">
			</td>
			<td>
				<span id="resultId">=</span>
			</td>
		    </tr>
		</table>
		
		<script type="text/javascript">
			function factorial(){
				var num = document.getElementById('numId').value;
				var result = 1;
				if(num < 0){
					var resultObj =document.getElementById('resultId');
					resultObj ="负数没有阶乘";
				}else if(num == 0){
					var resultObj =document.getElementById('resultId');
					resultObj ="1";
				}else{
					for (var i = num - 1; i > 1; i--) {
						result *= i;
					}
					var resultObj =document.getElementById('resultId');
					resultObj = result;
				}
			}
		</script>
		<table>
			<tr>
				<td>
					<input type="text" id="num"/>
				</td>
					
				<td><button onclick="fac()">阶乘</button>
				</td>
				<td>
					<span id="result">=</span>
				</td>
			</tr>
					
		</table>
		<script type="text/javascript">
			function fac(){
				var num = document.getElementById('num').value;
				var f = 1;
				if(num < 0){
					document.getElementById('result').innerHTML = "负数没有阶乘";
				}else if(num == 0){
					document.getElementById('result').innerHTML = "0的阶乘是1";
				}else{
					for(var i = 1;i <= num;i++){
						f *= i;
					}
					document.getElementById('result').innerHTML = '=' + f;
				}
			}
		</script>
	</body>
</html>